<script setup>
import { Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { policiesDeleteService, policiesGetListService } from '@/api/policies'
import { useRouter } from 'vue-router'

const router = useRouter()

const policiesList = ref([])

const onRowClick = (row) => {
  router.push(`/policies/show?id=${row.id}&name=${row.name}`)
}

const getPoliciesList = async () => {
  const res = await policiesGetListService()
  policiesList.value = res.data.data
}
getPoliciesList()

const deletePolicies = async (id) => {
  await ElMessageBox.confirm('你确认要删除该策略么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await policiesDeleteService(id)
  getPoliciesList()
}

</script>

<template>
  <el-container>
    <el-header
      style="
        border: 1px solid rgb(229, 229, 229);
        display: flex;
        flex-direction: row-reverse;
        align-items: center;
      "
    >
      <el-button
        @click="$router.push('/policies/create')"
        type="default"
        color="black"
        size="large"
        auto-insert-space
      >
        创建策略
      </el-button>
    </el-header>
    <el-main style="border: 1px solid rgb(229, 229, 229)">
      <el-table @row-click="onRowClick" :data="policiesList" highlight-current-row="true" row-style="cursor: pointer" v-loading="loading">
        <el-table-column prop="name" label="策略名"></el-table-column>
        <el-table-column>
          <template #default="scope">
            <div style="display: flex; justify-content: flex-end; align-items: center;">
              <el-icon size="20" @click.stop="deletePolicies(scope.row.id)"><Delete /></el-icon>
            </div>
          </template>
        </el-table-column>

        <template #empty>
          <el-empty description="没有策略"></el-empty>
        </template>
      </el-table>
    </el-main>
  </el-container>
</template>

<style scoped></style>
